//
//  PopoverAnimDelegate.swift
//  ZMWEIBO
//
//  Created by 庄猛 on 16/12/20.
//  Copyright © 2016年 BeiSheng. All rights reserved.
//

import UIKit

class PopoverAnimDelegate: NSObject {

    var myBlock : (Bool) -> ()

    init(tempBlock: @escaping (Bool) -> ()) {

        myBlock = tempBlock

    }

    deinit {
        print("消失le")
    }


}



extension PopoverAnimDelegate :UIViewControllerTransitioningDelegate{

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?{

        return PopPresentController(presentedViewController: presented, presenting: presenting)


    }

    //以下两个方法是告诉系统弹出和消失的操作有谁来管理,一旦实现系统就不会再给默认的弹出样式
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        myBlock(true)
        return self
    }


    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        myBlock(false)

        return self
    }

}


extension PopoverAnimDelegate:UIViewControllerAnimatedTransitioning{
    //返回动画时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval{


        return 0.25

    }

    // This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
    //    弹出消失动画代码在以下方法中处理
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning){

        //        let presentingViewVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        //
        //        let presentedViewVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        //        QL1(presentingViewVC)
        //        QL1(presentedViewVC)

        let presentingView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)

        //如果presentedView有值说明是弹出动画
        if let tempView = presentedView {
            transitionContext.containerView.addSubview(tempView)
            
            tempView.transform = CGAffineTransform(translationX: 0, y: KScreenH)

            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

                tempView.transform = CGAffineTransform.identity

                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })

        }else{


            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {

                presentingView!.transform = CGAffineTransform(translationX: 0, y: KScreenH)

                
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
            
            
            
            
        }
        
        
}

}
